/**
 * 自动处理JSON序列化和反序列化
 */
const localStorageUtil = {
  set(key, value) {
    localStorage.setItem(key, JSON.stringify(value));
  },

  /**
   * 获取localStorage
   */
  get(key) {
    const value = localStorage.getItem(key);
    return JSON.parse(value);
  },

  /**
   * 删除指定localStorage
   */
  remove(key) {
    localStorage.removeItem(key);
  },

  /**
   * 清空所有localStorage
   */
  clear() {
    localStorage.clear();
  },

  /**
   * 判断指定键是否存在
   */
  has(key) {
    return this.get(key) !== null;
  },
};

export default localStorageUtil;
